package com.hk.yajin.function.members.activity.setting;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseActivity;
import com.hk.yajin.function.MainActivity;
import com.hk.yajin.function.members.activity.RegisterUserNameActivity;
import com.hk.yajin.manager.UserInfoManager;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.url.UrlConstants;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.hk.yajin.R.id.tv_sure;

public class ChangeNickNameActivity extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_nickname)
    EditText tvNickname;
    @BindView(tv_sure)
    TextView tvSure;
    private String oldString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_nickname);
        ButterKnife.bind(this);
        intiView();
    }

    private void intiView() {
        initToolBar(R.string.changenickname);
        tvNickname.setText(UserInfoManager.getInstance().getUserInfo().getUser_name());
        tvNickname.setSelection(tvNickname.getText().toString().length());
        oldString = UserInfoManager.getInstance().getUserInfo().getUser_name();
    }


    @OnClick(tv_sure)
    public void onViewClicked() {
        final String name = tvNickname.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showShortToast("用户昵称不能为空");
            return;
        }

        if (isNetWorkConnect()) {
            showLoadingView();
            Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
            reqParam.put("user_name", name);
            HttpRequestUtils.post(false, UrlConstants.FUNCTION_GET_CHANGE_NICK_NAME, reqParam, false, new HttpCallBack() {
                @Override
                public void onSucced(String data) {
                    hideLoadingView();
                    showShortToast("设置成功");
                    UserInfoManager.getInstance().getUserInfo().setUser_name(name);
                    finish();
                }

                @Override
                public void onErrorCode(NewWorkBean bean) {
                    hideLoadingView();
                    showShortToast(bean.getMsg());
                }

                @Override
                public void onBadHttp(String tip) {
                    hideLoadingView();
                    showShortToast(tip);
                }

            });
        }


    }

    @Override
    public void onBackPressed() {
        if (oldString.equals(tvNickname.getText().toString())) {
            finish();
        } else {
            new AlertDialog.Builder(ChangeNickNameActivity.this).setTitle("提示")//设置对话框标题
                    .setMessage("昵称设置尚未完成，确定现在退出吗？")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            MainActivity.starItSelf(getApplicationContext(),MainActivity.class);
                        }
                    })
                    .setNegativeButton("取消", null)
                    .show();
        }

    }
}
